package com.softer.chengxin.traning.dao;

import com.softer.chengxin.traning.dao.provider.TraningDetailProvider;
import com.softer.chengxin.traning.entity.TraningDetail;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface TraningDetailDao {
    /**
     * 查询详情
     *
     * @param id
     * @return
     */
    @Select("SELECT remark,id,state,file_id as fileId,traning_id as traningId,raining_subject AS rainingSubject, " +
            "start_date AS startDate,end_date AS endDate,rec_remark as recRemark,rec_date as recDate,rec_name  as recName, " +
            "CASE state WHEN '0' THEN '保存' WHEN '1' THEN '审批中' WHEN '2' THEN  '审核通过' WHEN '2' THEN '审核不通过' END as stateName," +
            "edu_training_dep  as eduTrainingDep FROM traning_detail WHERE id = #{id}")
    TraningDetail getInfo(@Param("id") String id);

    /**
     * 新增
     */
    @Insert("INSERT INTO traning_detail(traning_id,state,raining_subject,edu_training_dep, end_date,remark,start_date,file_id,create_time) " +
            "VALUES(#{b.traningId},'0',#{b.rainingSubject},#{b.eduTrainingDep},#{b.endDate},#{b.remark},#{b.startDate},#{b.fileId},now())")
    int addTraning(@Param("b") TraningDetail traningDetail);

    /**
     * 查询新增
     *
     * @param id
     * @return
     */
    @Select("SELECT id,state,raining_subject AS rainingSubject,start_date AS startDate,end_date AS endDate," +
            "CASE state WHEN '0' THEN '保存' WHEN '1' THEN '审批中' WHEN '2' THEN '审核通过' WHEN '3' THEN '审核不通过' END as stateName," +
            "CASE edu_training_dep WHEN '1' THEN '市级及以上物业主管部门' WHEN '2' THEN '区（市）物业主管部门' WHEN '3' THEN '国家、省、市物业协会' END eduTrainingDep " +
            " FROM traning_detail WHERE traning_id = #{id} and FIND_IN_SET(state,#{state}) ORDER BY state")
    List<TraningDetail> getAllListByTranId(@Param("id") String id, @Param("state") String state);


    /**
     * 查询新增
     *
     * @param id
     * @return
     */
    @Select("SELECT id,state,raining_subject AS rainingSubject,start_date AS startDate,end_date AS endDate," +
            "CASE state WHEN '0' THEN '保存' WHEN '1' THEN '审批中' WHEN '2' THEN '审核通过' WHEN '3' THEN '审核不通过' END as stateName," +
            "CASE edu_training_dep WHEN '1' THEN '市级及以上物业主管部门' WHEN '2' THEN '区（市）物业主管部门' WHEN '3' THEN '国家、省、市物业协会' END eduTrainingDep" +
            " FROM traning_detail WHERE traning_id = #{id} and FIND_IN_SET(state,#{state}) and FIND_IN_SET(edu_training_dep,#{department}) ORDER BY state")
    List<TraningDetail> getAllListByTranIdAndDep(@Param("id") String id, @Param("state") String state, @Param("department") String department);

    /**
     * 修改
     *
     * @param traningDetail
     * @return
     */
    @UpdateProvider(type = TraningDetailProvider.class, method = "update")
    int update(@Param("traningDetail") TraningDetail traningDetail);

    /**
     * 删除
     *
     * @return
     */
    @Delete("DELETE FROM traning_detail WHERE id =#{id}")
    int del(String id);

    /**
     * 提交审核
     *
     * @param id
     * @param state
     * @return
     */
    @UpdateProvider(type = TraningDetailProvider.class, method = "changeState")
    int changeState(@Param("id") String id, @Param("state") String state, @Param("recRemark") String recRemark, @Param("name") String name);

    /**
     * @param id
     * @return
     */
    @Select("select count(1) from traning_detail where traning_id=#{id} and state='1' ")
    int selectStateByTraning(String id);

    /**
     * 查询培训人员的培训记录
     * 用于培训查看页面
     *
     * @param id traning_info ID
     * @return 培训记录
     */
    @Select("SELECT id,state,raining_subject AS rainingSubject,start_date AS startDate,end_date AS endDate," +
            " CASE state WHEN '0' THEN '保存' WHEN '1' THEN '审批中' WHEN '2' THEN '审核通过' WHEN '3' THEN '审核不通过' END as stateName," +
            "CASE edu_training_dep WHEN '1' THEN '市级及以上物业主管部门' WHEN '2' THEN '区（市）物业主管部门' WHEN '3' THEN '国家、省、市物业协会' END eduTrainingDep" +
            " FROM traning_detail WHERE traning_id = #{id} and state!=0 ORDER BY state")
    List<TraningDetail> getTrainingDetailList(int id);
}
